/**
 * 
 */
package com.cuc.platform.core.service.logic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;

import com.cuc.platform.core.componet.security.utils.CommonIdentityTools;
import com.cuc.platform.core.dao.baseInfo.UserInfoDao;
import com.cuc.platform.core.domain.baseInfo.UserInfo;
import com.cuc.platform.core.service.ServiceException;
import com.cuc.platform.core.service.UserInfoService;

/**
 * @author peepul
 *	
 * 用户信息业务实现类
 */
public class UserInfoServiceImpl implements UserInfoService {

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.UserInfoService#getUserInfo(java.lang.Long)
	 */
	@Override
	public UserInfo getUserInfo(Long id) {
		if(null == id) return null;

		return userInfoDao.getUserInfo(id);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.UserInfoService#getUserInfoWithOrg(java.lang.Long)
	 */
	@Override
	public UserInfo getUserInfoWithOrg(Long id) {
		if(null == id) return null;
		
		return userInfoDao.getUserInfoWithOrg(id);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.UserInfoService#getAllUserInfos()
	 */
	@Override
	public List<UserInfo> getAllUserInfos() {
		
		return userInfoDao.getAllUserInfos();
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.UserInfoService#insertUserInfo(com.cuc.platform.core.domain.UserInfo)
	 */
	@Override
	public void insertUserInfo(UserInfo userInfo) throws ServiceException {
		if(null == userInfo) return;
		
		//赋缺省值
		if(null == userInfo.getStatus()){
			userInfo.setStatus(CommonIdentityTools.IS_ACTIVE);
		}
		
		userInfoDao.insertUserInfo(userInfo);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.UserInfoService#updateUserInfo(com.cuc.platform.core.domain.UserInfo)
	 */
	@Override
	public void updateUserInfo(UserInfo userInfo) throws ServiceException {
		if(null == userInfo) return;
		
		//赋缺省值
		if(null == userInfo.getStatus()){
			userInfo.setStatus(CommonIdentityTools.IS_ACTIVE);
		}
		
		userInfoDao.updateUserInfo(userInfo);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.UserInfoService#delUserInfo(java.lang.Long)
	 */
	@Override
	public void delUserInfo(Long id) throws ServiceException {
		if(null == id) return;
		
		try{
			int rv = userInfoDao.delUserInfo(id);
			
			//如果返回值为0,则没找到对应id的数据，抛出业务异常给web层
			if(rv == 0){
				logger.warn("没找到id为：" + id + "的数据，无效的删除操作。");
				throw new ServiceException("系统中不存在要删除的数据！");
			}
		}catch(DataAccessException ex){
			logger.error("有外键关联的数据，无效的删除操作。");
			throw new ServiceException("请先删除关联数据，才能删除本条数据。");
		}
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.UserInfoService#updateStatus(java.lang.Long, java.lang.Integer)
	 */
	@Override
	public void updateStatus(Long id, Integer status) throws ServiceException {
		if((null == id) || (null == status)) return;
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", id);
		map.put("status", status);
		
		userInfoDao.updateStatus(map);
	}

	//日志
	private static Logger logger = Logger.getLogger(UserInfoServiceImpl.class);
	
	/**
	 * Ioc Beans
	 */
	private UserInfoDao userInfoDao;
	/**
	 * @param userInfoDao the userInfoDao to set
	 */
	public void setUserInfoDao(UserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}
}
